package po

import (
	"encoding/json"
	"time"
)

type Agent struct {
	Id         int64  `json:"id"`
	GUID       string `xorm:"guid" json:"guid"`
	OrganizeId int64  `xorm:"organize_id" json:"organize_id"`
	Platform   string `xorm:"platform" json:"platform"`
	Role       string `xorm:"role" json:"role"`
	AreaID     string `xorm:"area_id" json:"area_id"`
	//DetailRepoKey    string          `xorm:"detail_key" json:"-"`
	DetailRepoValue AgentDetail `xorm:"detail" json:"detail_repo_value"`
	//ConfigRepoKey    string          `xorm:"config_key" json:"-"`
	ConfigRepoValue AgentConfig `xorm:"config" json:"config_repo_value"`
	//RunTimeRepoKey   string          `xorm:"runtime_key" json:"-"`
	RunTimeRepoValue AgentRuntime `xorm:"runtime" json:"runtime_repo_value"`
	Created          time.Time    `xorm:"created" json:"created"`
	Updated          time.Time    `xorm:"updated" json:"updated"`
	Deleted          time.Time    `xorm:"deleted" json:"deleted"`
}

type AgentDetail struct {
	ClusterName string `json:"cluster_name" yaml:"cluster_name"`
	Area        Area   `json:"area" yaml:"area"`
	ParentGuid  string `json:"parent_guid"`
}

func (r *AgentDetail) FromDB(data []byte) error {
	return json.Unmarshal(data, r)
}

func (r *AgentDetail) ToDB() ([]byte, error) {
	return json.Marshal(r)
}

type AgentRuntime struct {
	ClusterNodes []AgentNodeInfo `json:"cluster_nodes" yaml:"cluster_nodes"`
}

func (r *AgentRuntime) FromDB(data []byte) error {
	return json.Unmarshal(data, r)
}

func (r *AgentRuntime) ToDB() ([]byte, error) {
	return json.Marshal(r)
}

type AgentNodeInfo struct {
	NodeName string `json:"node_name"`
	CpuCores string `json:"cpu_cores"`
	MemSize  string `json:"mem_size"`
}

type AgentConfig struct {
	HeartbeatInterval int64    `json:"heartbeat_interval" yaml:"heartbeat_interval"`
	RpcServices       []string `json:"rpc_services" yaml:"rpc_services"`
	MonitorComponents []string `json:"monitor_components"`
}

func (r *AgentConfig) FromDB(data []byte) error {
	return json.Unmarshal(data, r)
}

func (r *AgentConfig) ToDB() ([]byte, error) {
	return json.Marshal(r)
}
